<?php
class upload
{
	// public static $basedir = $_SERVER['DOCUMENT_ROOT'].'/pub';

	/** 
	 * Upload head-icon, and return file url.
	 *
	 * @param input string, html input[type=file]'s name.
	 * @param id    int, uploader id.
	 * @param db    Zend_Db_Adapter.
	 *
	 * @return string, indicate the file url. false if failed.
	 */
	public static function icon($input, $id, $db)
	{
		$basedir = $_SERVER['DOCUMENT_ROOT'].'/pub';

		$ti=time();
		$uploaddir=$basedir.'/icon/';
		$ext=basename($_FILES[$input]['name']);
		$ext=substr($ext, strrpos($ext, '.')+1);
		$name="$id.$ti.$ext";
		$pathname=$uploaddir.$name;
		if (move_uploaded_file($_FILES[$input]['tmp_name'], $pathname)) {
			
			$result=$db->query('select Icon from account '.
				'where AId=:aid', array('aid'=>$id));
			$rows=$result->fetchAll(Zend_Db::FETCH_ASSOC);
			$oldicon=$rows[0]['Icon'];
			if (!empty($oldicon) && $oldicon!='NULL') {
				// remove old icon
				unlink($uploaddir.$oldicon);
			}

			$db->query('update account '.
					'   set Icon=:icon where AId=:aid',
				array('icon'=>$name, 'aid'=>$id));
			
			return '/pub/icon/'.$name;
		}
		else 
			return false;
	}

	/** 
	 * Upload courseware, and return 1 or 0.
	 *
	 * @param input    string, html input[type=file]'s name.
	 * @param title    string, courseware name.
	 * @param uploader string, uploader name
	 * @param cid   int, course id.
	 * @param db    Zend_Db_Adapter.
	 *
	 * @return int, 1 means success, 0 means failed.
	 */
	static function courseware($input, 
							   $title,
							   $uploader,
							   $cid,
							   $db)
	{
		$basedir = $_SERVER['DOCUMENT_ROOT'].'/pub';

		$ti=time();
		$uploaddir=$basedir.'/courseware/';
		$ext=basename($_FILES[$input]['name']);
		$ext=substr($ext, strrpos($ext, '.')+1);
		$name="$cid.$ti.$ext";
		$pathname=$uploaddir.$name;

		if (move_uploaded_file($_FILES[$input]['tmp_name'], $pathname)) {
			
			$db->query('insert into courseware'.
					' (CId, Name, File, Uploader, `Date`) '.
					' values (:cid, :name, :file, :uploader, :date)',
				array('cid'=>$cid, 'name'=>$title,
					  'uploader'=>$uploader,
					  'file'=>$name, 'date'=>$ti));

			return 1;
		}
		else 
			return 0;
	}

	/** 
	 * Upload paper, and return 1 or 0.
	 *
	 * @param input string, html input[type=file]'s name.
	 * @param pid   int, paper id.
	 * @param db    Zend_Db_Adapter.
	 *
	 * @return int, 1 means success, 0 means failed.
	 */
	static function paper($input, $pid, $db)
	{
		$basedir = $_SERVER['DOCUMENT_ROOT'].'/pub';

		$ti = time();
		$uploaddir = $basedir.'/paper/';
		$ext = basename($_FILES[$input]['name']);
		$ext = substr($ext, strrpos($ext, '.')+1);
		$pid = intval($pid);
		$name = "$pid.$ti.$ext";
		$pathname = $uploaddir.$name;

		if (move_uploaded_file($_FILES[$input]['tmp_name'], $pathname)) {
			
			$result = $db->query("select Download from paper ".
								 " where PId=$pid");
			$rows = $result->fetchAll();
			$old = $rows[0]['Download'];
			if (!empty($old) && $old!='NULL') {
				// remove old paper file
				unlink($uploaddir.$old);
			}

			$db->query("update paper set Download=:download ".
					   " where PId=$pid", array('download'=>$name) );

			return 1;
		}
		else 
			return 0;
	}

	/** Upload teacher person space file(html or pic)
	 *
	 * @param input string, html input[type=file]'s name.
	 * @param user  string, user.
	 *
	 * @return int, 1 means success, 0 means failed.
	 */
	static function teacher($user, $input='file')
	{
		$uploaddir = $_SERVER['DOCUMENT_ROOT'].'/pub/teacher/'.$user.'/';
		
		$name = basename($_FILES[$input]['name']);
		$pathname = $uploaddir.$name;
		echo $pathname;
		
		if (move_uploaded_file($_FILES[$input]['tmp_name'], $pathname))
			return 1;
		else
			return 0;
	}

};
?>
